#include "raycaster_view_dialog.h"
#include "ui_raycaster_view_dialog.h"
#include <QPixmap>
#include <QImage>
#include "raycaster.h"
#include "scene.h"

RayCasterViewDialog::RayCasterViewDialog(const Scene *scene, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RayCasterViewDialog)
{
    Scene *scene_copy = scene->clone();
    ui->setupUi(this);
    this->setWindowTitle("Renderizado com raycasting");
    RayCaster raycaster;
    const CgColorBuffer *colors = raycaster.render(scene_copy, width(), height());
    unsigned char *imagedata = new unsigned char[height() * width() * 24];
    unsigned char *pixel_channel = imagedata;

    for(int i = 0 ; i < height() ; i++) {
        for(int j = 0 ; j < width() ; j++) {
            const CgColor &color = colors->getColor(i, j);
            *(pixel_channel++) = color.r() * 255;
            *(pixel_channel++) = color.g() * 255;
            *(pixel_channel++) = color.b() * 255;
        }
    }
    QPixmap pixmap = QPixmap::fromImage(QImage(imagedata, width(), height(), QImage::Format_RGB888));
    ui->viewport->setPixmap(pixmap);
    delete colors;
}

RayCasterViewDialog::~RayCasterViewDialog()
{
    delete ui;
}

void RayCasterViewDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
